<template>
  <view class="rows">
		<view class="top_bg">
			<image src="https://wt-1310486663.cos.ap-beijing.myqcloud.com/zgzj/jingzhang/static/images/bg5.png" mode="aspectFill" class="bg"></image>
			<view class="user">
				<image 
					:src="userInfo.avatarUrl || 'https://wt-1310486663.cos.ap-beijing.myqcloud.com/zgzj/jingzhang/static/images/avatar.png'" 
					mode="aspectFill">
				</image>
				<text>{{userInfo.nickname || '微信昵称'}}</text>
				<view class="update" @click="openLogin">
					<view class="icon">
						<image src="/static/images/edit.svg" mode="aspectFit"></image>
					</view>
					<text>更新资料</text>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="operate">
				<view 
					@click="toPage(item.path, {
						title: item.text
					})"
					class="operate-item" 
					v-for="(item, index) in operateList.filter((_, i) => i < 5)" 
					:key="index" >
					<view class="_left">
						<image :src="item.icon" mode="aspectFill"></image>
						<text>{{item.text}}</text>
					</view>
					<u-icon name="arrow-right" color="#000" bold top="2rpx" size="32rpx"></u-icon>
				</view>
			</view>
			<view class="operate">
				<view 
					@click="toPage(item.path)"
					class="operate-item" 
					v-for="(item, index) in operateList.filter((_, i) => i >= 5)" 
					:key="index" >
					<view class="_left">
						<image :src="item.icon" mode="aspectFill"></image>
						<text>{{item.text}}</text>
					</view>
					<u-icon name="arrow-right" color="#000" bold top="2rpx" size="32rpx"></u-icon>
				</view>
			</view>
		</view>
		<uni-popup ref="popupRef" type="center" :is-mask-click="false">
			<view class="popup">
				<!-- <view class="close" @click="$refs.popupRef.close()">
					<u-icon name="close" color="#4f4f4f" size="38rpx"></u-icon>
				</view> -->
				<view class="popup-title">请求获取您的微信授权</view>
				<image src="https://wt-1310486663.cos.ap-beijing.myqcloud.com/zgzj/jingzhang/static/images/logo.png" mode="aspectFit"></image>
				<view class="login-btn">
					<view class="login cancel" @click="$refs.popupRef.close()">暂不登录</view>
					<view class="login" @click="login">授权登录</view>
				</view>
				<view class="agreement" @click="agreement = !agreement">
					<view class="dot" :class="{active: agreement}"></view>
					<text>同意《用户协议》《隐私政策》</text>
				</view>
			</view>
		</uni-popup>
		<user-info-modal ref="userInfoRef" @confirm="update"></user-info-modal>
  </view>
</template>

<script>
	import {code2SessionAPI, getUserInfoByOpenidAPI, updateUserInfoAPI} from '@/api/index.js'
	import {toPage, showToast} from '@/utils/tools.js'
	import {getToken, setToken} from '@/utils/storageToken.js'
	export default {
    data() {
      return {
				agreement: false,
				userInfo: {},
				canIUseGetUserProfile: false,
        operateList: [
					{
						text: '我的消息',
						icon: '/static/images/user1.svg',
						path: '/packages/message/message'
					},{
						text: '我的收藏',
						icon: '/static/images/user2.svg',
						path: '/packages/empty/empty'
					},{
						text: '我的订单',
						icon: '/static/images/user3.svg',
						path: '/packages/order/order'
					},{
						text: '我的积分',
						icon: '/static/images/user4.svg',
						path: '/packages/empty/empty'
					},{
						text: '游客管理',
						icon: '/static/images/user5.svg',
						path: '/packages/tourists/tourists'
					},{
						text: '投诉建议',
						icon: '/static/images/user6.svg',
						path: '/packages/complaint/complaint'
					},{
						text: '联系我们',
						icon: '/static/images/user7.svg',
						path: '/packages/about/about'
					}
				]
      }
    },
		mounted() {
			this.getUserInfo();
		  if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true
			}
		},
		methods: {
			toPage,
			getUserInfo(){
				getUserInfoByOpenidAPI(this.openid).then(res => this.userInfo = res.result);
			},
			update(e){
				console.log(e);
				updateUserInfoAPI(e).then(res => {
					this.getUserInfo();
				})
			},
			openLogin(){
				console.log(1);
				this.openid = getToken();
				if(!this.openid) return this.$refs.popupRef.open();
				this.$refs.userInfoRef.open();
			},
			login(){
				if(!this.agreement) return showToast('请先阅读并同意用户协议');
				uni.showLoading({
					title: '登录中',
					mask: true
				});
				uni.login({
					success: (res) => {
						code2SessionAPI(res.code).then(res => {
							this.$refs.popupRef.close();
							setToken(res.result);
							this.$refs.userInfoRef.open();
						})
					}
				})
			}
		}
  }
</script>

<style lang="scss" scoped>
	.rows{
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		width: 100vw;
		height: 100%;
		background-color: #f7f7f7;
		.top_bg{
			position: relative;
			width: 750rpx;
			height: 580rpx;
			.bg{
				width: 100%;
				height: 100%;
			}
			.user{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				>image{
					width: 125rpx;
					height: 125rpx;
					border-radius: 50%;
					margin-bottom: 20rpx;
				}
				>text{
					font-size: 30rpx;
					color: $primary;
					margin-bottom: 20rpx;
				}
				.update{
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;
					height: 40rpx;
					border: 2rpx solid #fff;
					border-radius: 10rpx;
					color: #fff;
					font-size: $fs-30;
					background-color: transparent;
					border: 2rpx solid $primary;
					padding-right: 5rpx;
					padding-left: 38rpx;
					.icon{
						position: absolute;
						left: -10rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 45rpx;
						height: 45rpx;
						border-radius: 50%;
						background-color: $primary;
						image{
							width: 24rpx;
							height: 24rpx;
						}
					}
					text{
						color: $primary;
						font-size: $fs-24;
						line-height: 40rpx;
					}
				}
			}
		}
		.content{
			position: relative;
			flex: 1;
			.operate{
				position: relative;
				top: -100rpx;
				z-index: 1;
				background: #fff;
				border-radius: 20rpx;
				padding: 20rpx;
				box-shadow: 0 0 15rpx 2rpx rgba(0, 0, 0, .1);
				width: 688rpx;
				margin: 0 auto;
				margin-bottom: 20rpx;
				.operate-item{
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20rpx;
					border-bottom: 2rpx solid #f8f9fa;
					._left{
						display: flex;
						align-items: center;
						justify-content: center;
						image{
							width: 40rpx;
							height: 40rpx;
							margin-right: 20rpx;
						}
						text{
							font-size: 28rpx;
							color: #000;
						}
					}
					&:last-child{
						border-bottom: 0;
					}
				}
			}
			.rail{
				position: absolute;
				right: 0;
				bottom: 0;
				width: 750rpx;
				opacity: .4;
			}
			.tips{
				position: absolute;
				right: 50rpx;
				bottom: 70rpx;
				color: #b1afaf;
				font-size: 24rpx;
				width: 380rpx;
			}
		}
	}
	.popup{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 540rpx;
		height: 520rpx;
		background-color: #fff;
		border-radius: 20rpx;
		.close{
			position: absolute;
			top: 30rpx;
			right: 30rpx;
		}
		.popup-title{
			color: $black;
			font-size: $fs-36;
		}
		image{
			width: 200rpx;
			height: 180rpx;
			margin: 30rpx 0;
		}
		.login-btn{
			display: flex;
			align-items: center;
			justify-content: center;
			.login{
				padding: 15rpx 35rpx;
				color: $white;
				font-size: $fs-30;
				font-weight: bold;
				border-radius: 20rpx;
				background-color: $primary;
				&.cancel{
					background-color: $grey;
					margin-right: 20rpx;
				}
			}
		}
		.agreement{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;
			.dot{
				position: relative;
				width: 30rpx;
				height: 30rpx;
				border-radius: 50%;
				border: 2rpx solid #000;
				margin-right: 10rpx;
				&.active::before{
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					content: '';
					width: 15rpx;
					height: 15rpx;
					border-radius: 50%;
					background-color: #000;
				}
			}
			text{
				color: $black;
				font-size: $fs-30;
			}
		}
	}
</style>
